package com.sherlochao.controller;
/**
 * 分享的发布与显示
 */

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.sherlochao.model.SharePhoto;
import com.sherlochao.model.ShareTalk;
import com.sherlochao.model.ShareVideo;
import com.sherlochao.model.User;
import com.sherlochao.pojo.ShareTalkPOJO;
import com.sherlochao.service.SharePhotoService;
import com.sherlochao.service.ShareTalkService;
import com.sherlochao.service.ShareVideoService;
import com.sherlochao.service.UserService;

@Controller
@RequestMapping("/release")
public class ReleaseController {

	@Resource
	private ShareTalkService shareTalkService;

	@Resource
	private SharePhotoService sharePhotoService;

	@Resource
	private ShareVideoService shareVideoService;
	
	@Resource
	private UserService userService;

	public ReleaseController() {
		super();
	}

	@ResponseBody
	@RequestMapping(value = "/releaseShare", method = RequestMethod.POST)
	public String releaseShare(
			String shareTalkContent,
			String userId,
			@RequestParam(value = "file", required = false) MultipartFile[] file,
			HttpServletRequest request) throws Exception {

		ShareTalk shareTalk = new ShareTalk();
		shareTalk.setShareTalkContent(shareTalkContent);
		shareTalk.setUserId(userId);
		shareTalkService.save(shareTalk);

		System.out.println(shareTalk.toString());
		String shareTalkId = shareTalk.getId();
		
		// 获得物理路径webapp所在路径
		String pathRoot = request.getSession().getServletContext()
				.getRealPath("");
		String path = "";
		List<String> listImagePath = new ArrayList<String>();
		for (MultipartFile mf : file) {
			if (!mf.isEmpty()) {
				// 生成uuid作为文件名称
				String uuid = UUID.randomUUID().toString().replaceAll("-", "");
				// 获得文件类型
				String contentType = mf.getContentType();
				System.out.println(mf);
				System.out.println("-------------------------------------");
				System.out.println(contentType);
				// 获得文件后缀名称
				String name = contentType
						.substring(contentType.indexOf("/") + 1);
				if (name.equals("jpeg")) {
					path = "/sharePhoto/" + uuid + "." + name;
					mf.transferTo(new File(pathRoot + path));
					SharePhoto sharePhoto = new SharePhoto();
					sharePhoto.setSharePhotoAddress(pathRoot + path);
					sharePhoto.setShareTalkId(shareTalkId);
					System.out.println(pathRoot+path);
					// sharePhoto.setShareTalkId(); 获取发布的分享的id 未解决
					sharePhotoService.save(sharePhoto);
				} else {
					path = "/shareVideo/" + uuid + "." + name;
					mf.transferTo(new File(pathRoot + path));
					ShareVideo shareVideo = new ShareVideo();
					shareVideo.setShareVideoAddress(pathRoot + path);
					shareVideo.setShareTalkId(shareTalkId);
					System.out.println(pathRoot+path);
					// sharePhoto.setShareTalkId(); 获取发布的分享的id 未解决
					shareVideoService.save(shareVideo);
				}
				listImagePath.add(path);
			}
		}
		request.setAttribute("imagesPathList", listImagePath);
		return "sussess";
	}

	// 因为我的JSP在WEB-INF目录下面，浏览器无法直接访问
	@RequestMapping(value = "/forward")
	private String forward() {
		return "index";
	}
	
	@ResponseBody
	@RequestMapping(value = "/getShareMessage",method = RequestMethod.POST)
	public String getShareMessage(String shareTalkId){
		ShareTalkPOJO shareTalkPOJO = new ShareTalkPOJO();
		ShareTalk shareTalk = shareTalkService.get(shareTalkId);
		shareTalkPOJO.setId(shareTalkId);
		shareTalkPOJO.setShareTalkContent(shareTalk.getShareTalkContent());
		String userId = shareTalk.getUserId();
		User user = userService.get(userId);
		shareTalkPOJO.setUserName(user.getUsername());
		List<SharePhoto> sharePhoto = sharePhotoService.getSharePhotoByShareTalkId(shareTalkId);
		if(sharePhoto != null){
			ArrayList<String> sharePhotoAddressList = new ArrayList<String>();
			for(int i = 0;i < sharePhoto.size();i++)
				sharePhotoAddressList.add(sharePhoto.get(i).getSharePhotoAddress());
			shareTalkPOJO.setSharePhotoAddress(sharePhotoAddressList);
		}
		List<ShareVideo> shareVideos = shareVideoService.getShareVideoByShareTalkId(shareTalkId);
		if(shareVideos != null){
			ArrayList<String> shareVideoAddressList = new ArrayList<String>();
			for(int i=0;i<shareVideos.size();i++)
				shareVideoAddressList.add(shareVideos.get(i).getShareVideoAddress());
			shareTalkPOJO.setShareVideoAddress(shareVideoAddressList);
		}
		String json = JSON.toJSONString(shareTalkPOJO);
		return json;
	}

}
